package cyou.breathe.blog.knowledgebase.controller;

import cn.dev33.satoken.stp.StpUtil;
import cyou.breathe.blog.api.article.request.ArticleCreateRequest;
import cyou.breathe.blog.api.article.request.ArticleDeleteRequest;
import cyou.breathe.blog.api.article.request.ArticleUpdateRequest;
import cyou.breathe.blog.api.article.response.ArticleCreateResponse;
import cyou.breathe.blog.api.article.response.ArticleDeleteResponse;
import cyou.breathe.blog.api.article.response.ArticleUpdateResponse;
import cyou.breathe.blog.api.article.service.ArticleFacadeService;
import cyou.breathe.blog.api.folder.model.FolderVO;
import cyou.breathe.blog.api.folder.request.FolderCreateDirectoryRequest;
import cyou.breathe.blog.api.folder.request.FolderCreateRootRequest;
import cyou.breathe.blog.api.folder.request.FolderUpdateRequest;
import cyou.breathe.blog.api.folder.request.KnowledgeBaseDeleteFolderResponse;
import cyou.breathe.blog.api.folder.response.FolderCreateDirectoryResponse;
import cyou.breathe.blog.api.folder.response.FolderCreateRootResponse;
import cyou.breathe.blog.api.folder.response.FolderUpdateResponse;
import cyou.breathe.blog.api.folder.service.FolderFacadeService;
import cyou.breathe.blog.api.knowledgebase.request.KnowledgeBaseContentRequest;
import cyou.breathe.blog.api.knowledgebase.response.KnowledgeBaseContentResponse;
import cyou.breathe.blog.article.param.CreateArticleParam;
import cyou.breathe.blog.article.param.UpdateArticleParam;
import cyou.breathe.blog.knowledgebase.application.KnowledgeBaseApplicationService;
import cyou.breathe.blog.knowledgebase.infrastructure.exception.KnowledgeBaseErrorCode;
import cyou.breathe.blog.knowledgebase.param.CreateDirectoryParam;
import cyou.breathe.blog.knowledgebase.param.FolderUpdateParam;
import cyou.breathe.blog.web.vo.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: breathe
 * @createTime: 2025-09-06
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("knowledgeBase")
public class KnowledgeBaseController {

    @Autowired
    private KnowledgeBaseApplicationService knowledgeBaseApplicationService;

    @Autowired
    private ArticleFacadeService articleFacadeService;

    @Autowired
    private FolderFacadeService folderFacadeService;

    @GetMapping("/queryKnowledgeBases")
    public Result<FolderVO> queryAllFolderAndArticle(@RequestParam(value = "folderId", defaultValue = "0") Long folderId) {
        long userId = StpUtil.getLoginIdAsLong();
        KnowledgeBaseContentRequest request = new KnowledgeBaseContentRequest(userId, folderId);
        KnowledgeBaseContentResponse<FolderVO> response = knowledgeBaseApplicationService.queryAllFolder(request);

        if (!response.getSuccess()) {
            return Result.error(KnowledgeBaseErrorCode.KNOWLEDGE_BASE_GET_FAILED);
        }

        return Result.success(response.getData());
    }

    @GetMapping("/queryKnowledgeFolder")
    public Result<FolderVO> queryFolderAndArticle(@RequestParam(value = "folderId", defaultValue = "0") Long folderId) {
        long userId = StpUtil.getLoginIdAsLong();

        KnowledgeBaseContentRequest request = new KnowledgeBaseContentRequest(userId, folderId);
        KnowledgeBaseContentResponse<FolderVO> response = knowledgeBaseApplicationService.queryFolder(request);

        if (!response.getSuccess()) {
            return Result.error(KnowledgeBaseErrorCode.KNOWLEDGE_BASE_GET_FAILED);
        }

        return Result.success(response.getData());
    }

    @PostMapping("/createRootFolder")
    public Result<Long> createRootFolder(@RequestBody String folderName) {
        long userId = StpUtil.getLoginIdAsLong();

        FolderCreateRootRequest request = new FolderCreateRootRequest(folderName, userId);
        FolderCreateRootResponse response = knowledgeBaseApplicationService.createRootFolder(request);

        if (!response.getSuccess()) {
            return Result.error(KnowledgeBaseErrorCode.KNOWLEDGE_BASE_CREATE_ROOT_FOLDER_FAILED);
        }

        return Result.success(response.getFolderId());
    }

    @DeleteMapping("/deleteFolder")
    public Result<Long> deleteFolder(@RequestParam("folderId") Long folderId) {
        long userId = StpUtil.getLoginIdAsLong();

        KnowledgeBaseContentRequest request = new KnowledgeBaseContentRequest(userId, folderId);
        KnowledgeBaseDeleteFolderResponse response = knowledgeBaseApplicationService.deleteAllFolder(folderId, userId);

        if (!response.getSuccess()) {
            return Result.error(KnowledgeBaseErrorCode.KNOWLEDGE_BASE_DELETE_FOLDER_FAILED);
        }

        return Result.success(response.getFolderId());
    }

    @PutMapping("/updateFolder")
    public Result<Long> updateFolder(@RequestBody FolderUpdateParam param) {
        long userId = StpUtil.getLoginIdAsLong();

        FolderUpdateRequest request = new FolderUpdateRequest(param.getFolderId(), param.getFolderName(), param.getParentId(), userId);
        request.setUserId(userId);
        FolderUpdateResponse response = knowledgeBaseApplicationService.updateFolder(request);

        if (!response.getSuccess()) {
            return Result.error(KnowledgeBaseErrorCode.KNOWLEDGE_BASE_UPDATE_FOLDER_FAILED);
        }

        return Result.success(response.getFolderId());
    }

    @PostMapping("/createArticle")
    public Result<Long> createArticle(@RequestBody CreateArticleParam param) {
        long userId = StpUtil.getLoginIdAsLong();
        ArticleCreateRequest request = new ArticleCreateRequest(param.getFolderId(), param.getTitle(), param.getContent(), param.getTags(), param.getState(), userId);
        ArticleCreateResponse response = knowledgeBaseApplicationService.createArticle(request);
        if (!response.getSuccess()) {
            return Result.error(KnowledgeBaseErrorCode.KNOWLEDGE_BASE_CREATE_FAILED);
        }
        return Result.success(response.getArticleId());
    }

    @PutMapping("/updateArticle")
    public Result<Long> updateArticle(@RequestBody UpdateArticleParam param) {
        long userId = StpUtil.getLoginIdAsLong();
        ArticleUpdateRequest request = new ArticleUpdateRequest(param.getArticleId(), param.getFolderId(), param.getTags(), param.getTitle(), param.getContent(), userId, param.getState());
        ArticleUpdateResponse response = knowledgeBaseApplicationService.updateArticle(request);
        if (!response.getSuccess()) {
            return Result.error(KnowledgeBaseErrorCode.KNOWLEDGE_BASE_UPDATE_FAILED);
        }
        return Result.success(response.getArticleId());
    }

    @DeleteMapping("/deleteArticle")
    public Result<Long> deleteArticle(@RequestParam("articleId") Long articleId) {
        long userId = StpUtil.getLoginIdAsLong();
        ArticleDeleteRequest request = new ArticleDeleteRequest(articleId, userId);
        ArticleDeleteResponse response = knowledgeBaseApplicationService.deleteArticle(request);
        if (!response.getSuccess()) {
            return Result.error(KnowledgeBaseErrorCode.KNOWLEDGE_BASE_DELETE_FAILED);
        }

        return Result.success(response.getArticleId());
    }

    @PostMapping("/createDirectory")
    public Result<Long> createDirectory(@RequestBody CreateDirectoryParam param) {
        long userId = StpUtil.getLoginIdAsLong();
        FolderCreateDirectoryRequest request = new FolderCreateDirectoryRequest(param.getFolderName(), param.getParentId(), userId);
        FolderCreateDirectoryResponse response = folderFacadeService.createDirectory(request);

        if (!response.getSuccess()) {
            return Result.error(KnowledgeBaseErrorCode.KNOWLEDGE_BASE_CREATE_FAILED);
        }

        return Result.success(response.getFolderId());
    }
}